package sc.love.boot.service.iml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import sc.love.boot.mapper.UserMapper;
import sc.love.boot.pojo.User;
import sc.love.boot.pojo.UserExample;
import sc.love.boot.listener.UserListener;
import sc.love.boot.service.UserService;
import org.apache.dubbo.config.annotation.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description 用户实现实现类
 * @Author 15393
 * @Date 2020/5/25 22:49
 * @Version V1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    ApplicationContext applicationContext;

    @Override
    public User checkUser(String userNumber, String password) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNumberEqualTo(userNumber);

        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() == 0) {
            User user = new User();
            user.setUserNumber(userNumber);
            user.setPassword(password);
            user.setStatusDate(new Date());
            userMapper.insertSelective(user);
            return user;
        }
        applicationContext.publishEvent(new UserListener(this));
        return users.get(0);
    }
}